﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectStore30032013.PRESENTATION;

namespace ProjectStore30032013.PRESENTATION
{
    public partial class frm_danh_sach_hang_ban : Form
    {
        public frm_danh_sach_hang_ban()
        {
            InitializeComponent();
            set_form_event();
        }

        private void frm_danh_sach_hang_ban_Load(object sender, EventArgs e)
        {
            load_data_to_grv();
        }

        
        #region Members
        Supplier m_supplier;
        DataCSQLDataContext m_dtct_supplier = new DataCSQLDataContext();
        #endregion

        #region Data Structures

        #endregion


        #region Public Methods

        #endregion

        #region Private Methods
        private void load_data_to_grv()
        {
            DataCSQLDataContext v_dataCt = new DataCSQLDataContext();
            var select_supplire = from p in v_dataCt.Suppliers
                                  select new { p.SupplierID, p.CompanyName, p.Address, p.NumberNip, p.Regin };
            dgrv_dm_hang_ban.DataSource = select_supplire;
        }

        private void insert_obj()
        {
            frm_cap_nhat_hang_ban v_frm_hb = new frm_cap_nhat_hang_ban();
            v_frm_hb.Display();
            load_data_to_grv();
        }
        private void update_obj()
        {
            grv_to_obj();   
            frm_cap_nhat_hang_ban v_frm_hb = new frm_cap_nhat_hang_ban();
            v_frm_hb.Display(m_supplier.SupplierID);
            
            load_data_to_grv();
        }

        private void delete_row_hang_ban()
        {
            grv_to_obj();
            m_dtct_supplier.Suppliers.DeleteOnSubmit(m_supplier);
            m_dtct_supplier.SubmitChanges();
            load_data_to_grv();
        }
        private void grv_to_obj()
        {            
            string str_id = dgrv_dm_hang_ban.CurrentRow.Cells[1].Value.ToString();
            MessageBox.Show(str_id);
            m_supplier = (from c in m_dtct_supplier.Suppliers
                                   where c.SupplierID == str_id
                                   select c).Single();                  
        }
        #endregion

        #region Events
        private void set_form_event()
        {
            btn_thoat.Click += new EventHandler(btn_thoat_Click);
            this.KeyDown += new KeyEventHandler(frm_danh_sach_hang_ban_KeyDown);
            btn_thêm.Click += new EventHandler(btn_thêm_Click);
            btn_sua.Click += new EventHandler(btn_sua_Click);
            btn_xoa.Click += new EventHandler(btn_xoa_Click);
        }

        void btn_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                if(MessageBox.Show("", "Bạn có chắc chắn muốn xóa.", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK)
                delete_row_hang_ban();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btn_sua_Click(object sender, EventArgs e)
        {
            try
            {
                update_obj();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void frm_danh_sach_hang_ban_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.Escape:
                        this.Close();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void btn_thêm_Click(object sender, EventArgs e)
        {
            try
            {
                insert_obj();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void btn_thoat_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion



    }
}
